package com.alinesno.cloud.initializr.starter.gateway.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.AjaxResult;
import com.alinesno.cloud.initializr.starter.entity.InitializrDepGroupEntity;
import com.alinesno.cloud.initializr.starter.entity.InitiallizrDependenceEntity;
import com.alinesno.cloud.initializr.starter.service.IInitializrDepGroupService;
import com.alinesno.cloud.initializr.starter.service.IInitiallizrDependenceService;
import com.alinesno.cloud.rest.gateway.adapter.page.TableDataInfo;
import com.alinesno.cloud.rest.gateway.adapter.plugins.TranslateCode;
import com.alinesno.cloud.rest.gateway.adapter.rest.BaseRestAdapter;

/**
 * <p> 前端控制器 </p>
 *
 * @author ${author}
 * @since 2020-11-20 10:06:08
 */
@RestController
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("com/alinesno/cloud/initializr/initiallizrDependence")
public class InitiallizrDependenceRest extends BaseRestAdapter<InitiallizrDependenceEntity, IInitiallizrDependenceService> {

    //日志记录
    private static final Logger log = LoggerFactory.getLogger(InitiallizrDependenceRest.class);

    @Autowired
    private IInitiallizrDependenceService initiallizrDependenceService;

    @Autowired
    private IInitializrDepGroupService depGroupService;



    @SuppressWarnings("unchecked")
	@TranslateCode(value = "[{hasStatus:has_status}]")
    @ResponseBody
    @PostMapping("/datatables")
    public TableDataInfo datatables(HttpServletRequest request, Model model, DatatablesPageBean page) {
        log.debug("page = {}", ToStringBuilder.reflectionToString(page));
        TableDataInfo bean = this.toPage(model, this.getFeign(), page);
        List<InitiallizrDependenceEntity> dependenceEntities = (List<InitiallizrDependenceEntity>) bean.getRows();
        dependenceEntities.stream().forEach(dep -> {
            InitializrDepGroupEntity groupEntity = depGroupService.findById(dep.getDepBussGroup());
            if (groupEntity != null) {
                dep.setDepBussGroupName(groupEntity.getDepGroupName());
            }
        });
        return bean;
    }

    /*回显组*/
	@TranslateCode(value = "[{hasStatus:has_status}]")
    @ResponseBody
    @PostMapping("/rebackGroupInfo")
    public AjaxResult rebackGroupInfo(HttpServletRequest request, Model model, DatatablesPageBean page) {
        List<InitializrDepGroupEntity> depGroupEntities = depGroupService.findAll();
        return AjaxResult.success(depGroupEntities);
    }



    @Override
    public IInitiallizrDependenceService getFeign() {
        return this.initiallizrDependenceService;
    }

//    @Override
//    public void add(Model model, HttpServletRequest request) {
//        RpcWrapper<InitializrDepGroupEntity> restWrapper = RpcWrapper.build();
//        restWrapper.eq("hasStatus", HasStatusEnums.LEGAL.value);
//        model.addAttribute("dep_groups", depGroupService.findAll(restWrapper));
//        super.add(model, request);
//    }
//
//    @Override
//    public void modify(HttpServletRequest request, Model model, String id) {
//        RpcWrapper<InitializrDepGroupEntity> restWrapper = RpcWrapper.build();
//        restWrapper.eq("hasStatus", HasStatusEnums.LEGAL.value);
//        model.addAttribute("dep_groups", depGroupService.findAll(restWrapper));
//        super.add(model, request);
//        super.modify(request, model, id);
//    }



}


























